﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using Library.Bll;
using Library.Common;

namespace Main.Controllers
{
    public class QueryController : Controller
    {
        //
        // GET: /Query/

        public int _timeoutMinute = ConfigurationManager.AppSettings["TimeoutMinute"] != null
                ? int.Parse(ConfigurationManager.AppSettings["TimeoutMinute"].ToString()) : 20;
        public int _pageSize = ConfigurationManager.AppSettings["QueryPageSize"] != null
                ? int.Parse(ConfigurationManager.AppSettings["QueryPageSize"].ToString()) : 10;
        int _Validity_Period = ConfigurationManager.AppSettings["smsInterval"] != null
            ? int.Parse(ConfigurationManager.AppSettings["smsInterval"].ToString()) : 60;

        public ActionResult Default()
        {
            int smsInterval = ConfigurationManager.AppSettings["smsInterval"] != null
                ? int.Parse(ConfigurationManager.AppSettings["smsInterval"].ToString()) : 120;
            ViewData["smsInterval"] = smsInterval;

            return View();
        }

        [HttpPost]
        public ActionResult Validate(FormCollection fc)
        {
            string idCard = fc["txtIdCard"].Trim().Replace("'", "");
            string phone = fc["txtPhone"].Trim().Replace("'", "");
            string vcode = fc["txtVCode"].Trim().Replace("'", "");

            //if (Session["_RegMobile"] == null || Session["_RegValCode"] == null)
            //{
            //    return Content("<font color='red'>验证码错误或者已过期，请刷新页面重新获取。</font>");
            //}
            //if (phone != Session["_RegMobile"].ToString())
            //{
            //    return Content("<font color='red'>请使用接收校验码的手机号查询。</font>");
            //}
            //if (vcode != Session["_RegValCode"].ToString())
            //{
            //    return Content("<font color='red'>校验码输入有误，请重新输入。</font>");
            //}

            string table = "Loan_Validate";
            string colunm = "*";
            string condition = string.Format("isvalid=1 and phone = '{0}' and vcode='{1}'"
                , phone, vcode);
            List<Dictionary<string, object>> ls1 = CommonBLL.GetList(table, colunm, "CreateDate desc", condition);

            condition = string.Format("isvalid=1 and phone = '{0}' and vcode='{1}' and datediff(ss,createdate,getdate())<{2}"
                , phone, vcode, _Validity_Period);
            List<Dictionary<string, object>> ls2 = CommonBLL.GetList(table, colunm, "CreateDate desc", condition);

            if (ls2.Count > 0)
            {
                return Content("3");
                //return Redirect("/query/list");
            }
            else if (ls1.Count > 0 && ls2.Count <= 0)
            {//验证码过期
                return Content("5");
            }
            else
            {// 验证码错误
                //return View("~/Views/Query/sorry.cshtml", 1);                
                return Content("4"); 
            }
        }
        public ContentResult VCodeValidate(string vcode)
        {
            if (Session["_RegValCode"] == null || Session["_RegValCode"].ToString() != vcode)
            {
                return Content("0");
            }
            return Content("1");
        }

        public ActionResult List(int? p)
        {
            Session["_RegIdCard"] = "37068719820103387X";
            Session["_RegMobile"] = "13811270621";
            Session["_RegValCode"] = "13811270621";
            if (Session["_RegIdCard"] == null || Session["_RegMobile"] == null)
            {
                //return Content("<font color='red'>此次查询已过期，请返回重新查询。</font>");
                return View("~/Views/Query/sorry.cshtml", 2);
            }
            /*
            string tableV = "Loan_Validate";
            string colunmV = "*";
            string conditionV = string.Format("isvalid=1 and phone = '{0}' and vcode='{1}' and datediff(mi,createdate,getdate()) < {2} "
                , Session["_RegMobile"], Session["_RegValCode"], _timeoutMinute);
            List<Dictionary<string, object>> ls = CommonBLL.GetList(tableV, colunmV, "CreateDate desc", conditionV);
            if (ls.Count < 1)
            {
                return View("~/Views/Query/timeout.cshtml", 1);
            }
            */
            //if (phone != Session["_RegMobile"].ToString())
            //{
            //    return Content("<font color='red'>请使用接收校验码的手机号查询。</font>");
            //}
            //if (vcode != Session["_RegValCode"].ToString())
            //{
            //    return Content("<font color='red'>校验码输入有误，请重新输入。</font>");
            //}

            string table = "tmp_Loan_InfoList";
            string columns = "[客户名称],[业务ID],[放款日期],[还款方式],[月利率],[贷款金额],[贷款期限],[项目经理],[联系电话]";
            string condition = string.Format("[idCard]='{0}' and [phone]='{1}'", Session["_RegIdCard"], Session["_RegMobile"]);
            if(p == null)
                p =1;
            int page = (int)p;
            PagedInfo pi = new PagedInfo(page, _pageSize);
            PagedResult<DataSet> ds = CommonBLL.GetPagedList(table, columns, "[业务ID] desc", condition, pi);
            //List<Dictionary<string, object>> ls = CommonBLL.GetList(table, columns, "[业务ID] desc", condition);
            if (ds.Result.Tables.Count > 0 && ds.Result.Tables[0].Rows.Count > 0)
            {
                return View("~/Views/Query/List.cshtml", ds);
            }
            else
            {
                //return Content("<font color='red'>查不到相关贷款记录，请验证输入的身份证号和手机号码是否正确，有任何疑问欢迎联系瀚华客服。</font>");
                return View("~/Views/Query/sorry.cshtml", 9);
            }
        }

        public ActionResult GetList(string idCard, string phone, string vcode)
        {
            idCard = idCard.Trim().Replace("'", "");
            phone = phone.Trim().Replace("'", "");
            vcode = vcode.Trim().Replace("'", "");
            if (Session["_RegMobile"] == null || Session["_RegValCode"] == null)
            {
                return Content("<font color='red'>验证码错误或者已过期，请刷新页面重新获取。</font>");
            }
            if (phone != Session["_RegMobile"].ToString())
            {
                return Content("<font color='red'>请使用接收校验码的手机号查询。</font>");
            }
            if (vcode != Session["_RegValCode"].ToString())
            {
                return Content("<font color='red'>校验码输入有误，请重新输入。</font>");
            }
            string table = "tmp_Loan_InfoList";
            string columns = "[客户名称],[业务ID],[放款日期],[还款方式],[月利率],[贷款金额],[贷款期限],[项目经理],[联系电话]";
            string condition = string.Format("[idCard]='{0}' and [phone]='{1}'", idCard, phone);
            List<Dictionary<string, object>> ls = CommonBLL.GetList(table, columns, "[业务ID] desc", condition);
            if (ls.Count > 0)
            {
                return PartialView("~/Views/Query/Shared/List.cshtml", ls);
            }
            else
            {
                return Content("<font color='red'>查不到相关贷款记录，请验证输入的身份证号和手机号码是否正确，有任何疑问欢迎联系瀚华客服。</font>");
            }
        }

        public ActionResult GetDetails(string bId)
        {
            string table, columns, condition;
            //string table = "tmp_Loan_InfoList";
            //string columns = "[客户名称],[业务ID],[还款方式],[月利率],[贷款金额],[贷款期限]";
            //string condition = string.Format("[业务ID]='{0}'", bId);
            //Dictionary<string, object> dInfo = CommonBLL.GetModel(table, columns, condition);

            table = "tmp_Loan_RepaymentScheme";
            columns = @"[客户名称],[业务ID],[期数],[还款日期],[还款本金],[还款利息],[还款总计]";
            condition = string.Format("[业务ID]='{0}'", bId.Trim().Replace("'", ""));
            List<Dictionary<string, object>> lsScheme = CommonBLL.GetList(table, columns, "[还款日期] asc", condition);

            table = "tmp_Loan_RepaymentRecord";
            columns = @"[客户名称],[业务ID],[还款日期],[还款本金],[还款利息],[逾期罚款],[到期日期],[总额]";
            condition = string.Format("[业务ID]='{0}'", bId);
            List<Dictionary<string, object>> lsRecord = CommonBLL.GetList(table, columns, "[还款日期] asc", condition);

            Dictionary<string, object> d = new Dictionary<string, object>();
            //d["info"] = dInfo;
            d["scheme"] = lsScheme;
            d["record"] = lsRecord;
            //ViewData["dInfo"] = dInfo;
            //ViewData["lsScheme"] = lsScheme;
            //ViewData["lsRecord"] = lsRecord;

            return PartialView("~/Views/Query/Details.cshtml", d);
        }
    }
}
